package com.company.cloud.user.modules.mgr.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.company.cloud.common.bean.LoginSimpleUserInfo;
import com.company.cloud.user.modules.api.request.RegisterNumDataRequest;
import com.company.cloud.user.modules.app.models.UserCountDetail;
import com.company.cloud.user.modules.base.entity.UserInfo;
import com.company.cloud.user.modules.base.service.UserInfoService;
import com.company.cloud.user.modules.cache.models.CustomerCountModel;
import com.company.cloud.user.modules.mgr.controller.req.UserCountQueryParam;
import com.company.cloud.user.modules.mgr.mapper.UserCountMapper;
import com.company.cloud.user.modules.mgr.service.UserCountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;

@Service
public class UserCountServiceImpl implements UserCountService {

	@Autowired
	private UserInfoService userInfoService;
	@Resource
	private UserCountMapper userCountMapper;

	@Override
	public Integer getUserCount(UserCountQueryParam param, String companyNo, LoginSimpleUserInfo userInfo) {
		QueryWrapper<UserInfo> qw = new QueryWrapper<UserInfo>().eq("company_no", companyNo)
				.eq(StringUtils.hasText(param.getUserType()),"user_type_one", param.getUserType())
				.ge(StringUtils.hasText(param.getStartDate()),"cdt",param.getStartDate())
				.le(StringUtils.hasText(param.getEndDate()),"cdt",param.getEndDate());
		if (userInfo!=null && userInfo.getId()!=null) {
			qw.eq("referral_user_id", userInfo.getId());
		}
		return userInfoService.count(qw);
	}

	@Override
	public UserCountDetail getUserCountDetail(String userType, String startDate, String endDate, String companyNo, boolean all, LoginSimpleUserInfo userInfo) {
		Integer daysCount = null;
		Integer allCount = null;
		daysCount = getUserCount(new UserCountQueryParam(userType, startDate,endDate),
				companyNo, userInfo);
		if (all) {
			allCount = getUserCount(new UserCountQueryParam(userType, null, null),
					companyNo, userInfo);
		}
		return new UserCountDetail(daysCount, allCount);
	}

	@Override
	public CustomerCountModel getCustomerCount(Integer userId) {
		CustomerCountModel model = userCountMapper.getCustomerCount(new QueryWrapper<CustomerCountModel>()
				.eq("referral_user_id", userId)
				.groupBy("referral_user_id")
		);
		return model;
	}

	@Override
	public List<CustomerCountModel> getCustomerCount(Set<Integer> userId) {
		List<CustomerCountModel> list = userCountMapper.getCustomerCountList(new QueryWrapper<CustomerCountModel>()
				.in("referral_user_id", userId)
				.groupBy("referral_user_id"));
		return list;
	}

	@Override
	public Integer userNum(RegisterNumDataRequest request) {
		int num= userInfoService.count(new QueryWrapper<UserInfo>()
				.eq(StrUtil.isNotEmpty(request.getCompanyNo()),"company_no",request.getCompanyNo())
				.eq(StrUtil.isNotEmpty(request.getUserOneType()),"user_type_one",request.getUserOneType())
				.ge(StringUtils.hasText(request.getStartDate()),"cdt",request.getStartDate())
				.le(StringUtils.hasText(request.getEndDate()),"cdt",request.getEndDate())

		);
		return num;
	}

}
